<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form action="" class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" class="layui-input" name="title" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">URL</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" class="layui-input" name="href" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn" lay-submit
                                    type="submit"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script id="toolbarDemo" type="text/html">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table aria-describedby="mydesc" class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script id="currentTableBar" type="text/html">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script th:inline="none">
    layui.use(['form', 'table', 'miniPage', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: '/admin/menu/menuList',
            parseData: function (result) {
                return {
                    "code": 0, //解析接口状态
                    "msg": result.msg, //解析提示文本
                    "count": result.data.total, //解析数据长度
                    "data": result.data.list //解析数据列表
                };
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '菜单列表',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {field: 'id', title: 'ID', sort: true},
                    {field: 'parentId', title: 'parentId', sort: true},
                    {field: 'title', title: '名称', sort: false},
                    {field: 'href', title: 'URL', sort: false},
                    {field: 'sort', title: '排序', sort: true},
                    {field: 'icon', title: 'icon', sort: false},
                    {field: 'target', title: 'target', sort: true},
                    {
                        field: 'type', title: 'type', templet: function (item) {
                            return item.type == 0 ? '目录' : item.type == 1 ? '菜单' : item.type == 2 ? '按钮' : item.type == 3 ? '页面' : '接口';
                        }
                    },
                    {field: 'perms', title: 'perms', sort: false},
                    {title: '操作', toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            escape: true,
            skin: 'row',
            even: true,
            method: 'post'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    title: data.field.title,
                    href: data.field.href
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('/admin/menu-add');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '添加菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('/admin/menu-edit?menuId=' + data.id);
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function (layero, index) {
                        $('input[name=title]').val(data.title);
                        $('input[name=href]').val(data.href);
                        $('input[name=parentId]').val(data.parentId);
                        $('input[name=sort]').val(data.sort);
                        $('input[name=icon]').val(data.icon);
                        $("input[name=target][value='" + data.target + "']").attr("checked", true);
                        $("input[name=type][value='" + data.type + "']").attr("checked", true);
                        $("input[name=perms][value='" + data.perms + "']").attr("checked", true);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    $.post('/admin/menu/menuDelete', {id: obj.data.id}, function (res) {
                        layer.alert(res.msg);
                        if (res.success) {
                            table.reload('currentTableId');
                        }
                    })
                });
            }
        });

    });
</script>